let laypage = layui.laypage
let form = layui.form

//执行一个laypage实例
let data = {
    pagenum: 1,
    pagesize: 2,
}
function renderariticle() {
    $.ajax({
        url: '/my/article/list',
        data: data,
        success: function (res) {
            let arr = []
            res.data.forEach(item => {
                arr.push(` <tr>
                <td>${item.title}</td>
                <td>${item.cate_name}</td>
                <td>${item.pub_date}</td>
                <td>${item.state}</td>
                <td>
                    <a href="./edit.html?id=${item.id}" class="layui-btn layui-btn-warm layui-btn-xs editbtn">编辑</a>
                    <button type="button" data-id=${item.id} class="layui-btn layui-btn-danger layui-btn-xs delbtn">删除</button>
                </td>
            </tr>`)
            })
            $('tbody').html(arr.join(''))
            showpage(res.total)
        }
    })
}
renderariticle()

// 分页
function showpage(t) {
    laypage.render({
        elem: 'page',
        count: t,
        limit: data.pagesize,
        curr: data.pagenum,
        limits: [3, 5, 7, 9],
        layout: ['count', 'limit', 'prev', 'page', 'next', 'skip'],
        jump: function (obj, first) {
            // 如果不是第一次加载，
            if (!first) {
                data.pagenum = obj.curr
                data.pagesize = obj.limit
                renderariticle()
            }
        }
    })
}


// 筛选******************************************
$.ajax({
    url: '/my/category/list',
    success: function (res) {
        let arr = []
        res.data.forEach(item => {
            arr.push(`<option value="${item.id}">${item.name}</option>`)
        })
        $('select[name=category]').append(arr.join(''))
        // 更新渲染
        form.render('select')
    }
})
// 筛选按钮
$('#search').on('submit', function (e) {
    e.preventDefault()
    let state = $('select[name=state]').val()
    let cate_id = $('select[name=category').val()
    if (state) {
        data.state = state
    } else {
        delete data.state
    }
    if (cate_id) {
        data.cate_id = cate_id
    } else {
        delete data.cate_id
    }
    data.pagenum = 1

    renderariticle()

})
// 删除操作
$('tbody').on('click', '.delbtn', function () {
    let iddel = $(this).data().id
    console.log(iddel);
    $.ajax({
        url: '/my/article/delete/' + iddel,
        success: function (res) {
            layer.msg(res.message)
            renderariticle()
        }
    })
})
























// 筛选************************************************************
$.ajax({
    url: '/my/category/list',
    success: function (res) {
        let arr = []
        res.data.forEach(item => {
            arr.push(``)
        });
    }
})